package com.bonc.modules.job.task;

import cn.hutool.core.date.DateUtil;
import com.bonc.modules.sys.service.OriginUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.stereotype.Component;

import java.util.concurrent.locks.Lock;

/**
 * @author jinlong
 * @date 2021/6/30
 */
@Slf4j
@Component("syncUserTask")
public class SyncUserTask implements ITask{

    private static final String LOCK_NAME = "sync_user_task";

    @Autowired
    private RedisLockRegistry redisLockRegistry;

    @Autowired
    private OriginUserService originUserService;

    @Override
    public void run(String params) throws Exception {

        Lock lock = redisLockRegistry.obtain(LOCK_NAME);
        boolean isLock = lock.tryLock();
        if (isLock) {
            try {
                log.info("用户同步:开始time:{}", DateUtil.date());


                originUserService.syncUser();
//                Date curDateTime = DateUtil.date();
//                Date dealDate = DateUtil.beginOfDay(DateUtil.offsetDay(curDateTime, -1));
//                List<VehicleTravelStatc> vehicleTravelStatcList = vehicleTravelRecdService.statc(dealDate);
//                VehicleTravelStatcParam vehicleTravelStatcParam = new VehicleTravelStatcParam();
//                vehicleTravelStatcParam.setTravelDate(dealDate);
//                vehicleTravelStatcService.delete(vehicleTravelStatcParam);
//                vehicleTravelStatcService.addBatch(vehicleTravelStatcList);

                log.info("用户同步:结束time:{}", DateUtil.date());
            } finally {
                lock.unlock();
            }
        }

    }
}
